package com.psm.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.*;
import com.psm.util.MD5;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheam on 15/6/19.
 */
@Before(IndexInterceptor.class)
public class RoleController extends BaseController<SysUser> {
    public Logger log=Logger.getLogger(getClass());

    public void index(){

        render("index.html");
    }


    public void listJson(){

        Record user = getCurrentUser();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        String code = this.getPara("code", "");
        String name = this.getPara("name", "");
        String dep_id = this.getPara("dep_id", "");

        if(!"".equals(code)){
            where.append(" and t.code like '%"+code+"%'");
        }

        if(!"".equals(name)){
            where.append(" and t.name like '%"+name+"%'");
        }


        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select t.* ", "from " + SysRole.tableName + " t  where 1=1 " + where.toString(), param.toArray());
        DataGrid dg = new DataGrid();
        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);

    }

    public void edit() {
        try {
            Long id = getParaToLong("id");
            SysRole role = SysRole.me.findById(id);
            this.setAttr("po",role);
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }

    public void save(){
         String id = getPara("id","");
         String code = getPara("code","");
         String name = getPara("name","");
         String status = getPara("status","0");

        SysRole role = null;
        if("".equals(id)){
            role = new SysRole();
        }else{
            role = SysRole.me.findById(id);
        }
        role.set("code",code);
        role.set("name",name);

        role.set("status",status);

        if("".equals(id)){
            role.save();
        }else{
            role.update();
        }

        this.rendJson_(1, "操作成功");
    }


    public void load_user_role(){
        String user_id = getPara("user_id","");

        List<Record> is_admin = SysRole.me.getRoleForUser(user_id);
        List<Record> record;
        if(is_admin.size()>0){
            record = SysRole.me.getUserRole(user_id);
        }else{
            record = SysRole.me.getUserRoleForOtherAdmin(user_id);
        }

        this.setAttr("user_id",user_id);
        this.setAttr("record",record);

    }

    public void save_user_role(){
        String[] role = getParaValues("role");

        String user_id = getPara("user_id", "");

        SysRefUserRole.me.delete_for_user(user_id);

        for (int i =0;i<role.length;i++){
            SysRefUserRole new_role = new SysRefUserRole();

            new_role.set("sys_user_id",user_id);
            new_role.set("sys_role_id",role[i]);

            new_role.save();
        }

        this.rendJson_(1, "操作成功");

    }

    public void set_resource(){
        setAttr("role_id",getPara("id"));
    }

    public void update_resource(){
        String role_id = getPara("role_id");
        String resources = getPara("select_role");

        Db.deleteById(SysRefRoleResource.tableName, "sys_role_id", role_id);

        String [] resource = resources.split(",");
        for (int i=0;i<resource.length;i++){
            SysRefRoleResource re = new SysRefRoleResource();
            re.set("sys_role_id",role_id);
            re.set("sys_resource_id",resource[i]);
            re.save();
        }

        this.rendJson_(1, "操作成功");

    }

    public void load_role_resource_tree(){
        String role_id = getPara("role_id","");
        List<Record> list = SysRole.me.load_resource(role_id);

        renderJson(list);
    }

}
